http,net,stream: optimize outgoing write paths - #64987
Conversation
Signed-off-by: GetThatCookie <NimmenKeks@gmx.de>
|
Review requested:
|
|
I haven't reviewed this in full yet (thanks for breaking up #64980, but this PR is still massive) but I've done a quick skim. For the chunk writing improvements (independent of corking) I don't think most of this is necessary. There is a big improvement here in combining HTTP chunks within the same tick, which is great, but most of the rest of the code seems to be managing merging low-level calls which we already do internally anyway. Testing on node without these changes, repeated writes here are already just one const http = require('http');
const net = require('net');
const origWritev = net.Socket.prototype._writev;
net.Socket.prototype._writev = function (chunks, cb) {
if (this.trackWrites) {
console.log(`_writev called with ${chunks.length} chunks on socket ${this.remoteAddress}:${this.remotePort}`);
}
return origWritev.call(this, chunks, cb);
};
const origWrite = net.Socket.prototype._write;
net.Socket.prototype._write = function (data, encoding, cb) {
if (this.trackWrites) {
console.log(`_write called with ${data.length} bytes on socket ${this.remoteAddress}:${this.remotePort}`);
}
return origWrite.call(this, data, encoding, cb);
};
const server = http.createServer((req, res) => {
req.socket.trackWrites = true;
for (let i = 0; i < 16; i++) res.write('x'.repeat(64));
res.end();
});
server.listen(0, () => {
http.get({ port: server.address().port }, (res) => {
let bytes = 0;
res.on('data', (d) => bytes += d.length);
res.on('end', () => server.close());
});
});Shows a single Can you try simplifying this to combine chunked writes (in terms of transfer-encoding chunks I mean) but without all the writev internal stream changes? I think you'll find you get roughly identical perf boost with 10% of the code changes. That's separate from the cork fix, I'll look at that closer but it would be helpful to clean this up first so the remaining logic is easier to review. |
This PR is one of four focused changes split out of #64980 following review
feedback.
The full context, rationale, related work, benchmarks, and validation details
are documented there.
The AI-assistance disclosure in #64980 applies to this split PR as well.